AVL树C++实现

用类封装AVL的几个属性,应该没有问题,如果有错请告知我,谢谢

其中打印树参考http://www.cnblogs.com/skywang12345/p/3577479.html

头文件

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

void testavl();


主函数

#include "Header.h"

int main()
{
	testavl();
	return 0;
}


AVL类实现

#include "Header.h"

struct node
{
	node* leftchild;
	node* rightchild;
	node* parents;
	int key;
	node(){};
	node(int a){ leftchild = NULL; rightchild = NULL; parents = NULL; key = a; };
};

class AVL
{
public:
	AVL();
	node* root_node;
	int Height(node* nd);
	void Insert(node* nd);
	void LeftRotate(node* nd);
	void RightRotate(node* nd);
	node* MinChild(node* nd);
	void Transplant(node *u, node *v);
	void Delete(node* nd);
	void Delete(int value);
	void InsertFixup(node* nd);
	void DeleteFixup(node* nd);
};

AVL::AVL()
{
	root_node = NULL;//用哨兵编程会简单一点,写的时候没有用哨兵
	
}




inline int max(int x, int y)
{
	return x > y ? x : y;
}

node* AVL::MinChild(node* nd) //返回以nd为根节点的子树的关键词最小节点
{
	node* min;
	min = NULL;
	while (nd)
	{
		min = nd;
		nd = nd->leftchild;
	}

	return min;
}

void AVL::Delete(node* nd)
{
	if (nd->leftchild == NULL && nd->rightchild == NULL)
	{
		if (nd->parents == NULL)
		{
			root_node == NULL;
		}
		else
		{
			if (nd == nd->parents->leftchild)
			{
				nd->parents->leftchild = NULL;
			}
			else
			{
				nd->parents->rightchild = NULL;
			}
			DeleteFixup(nd->parents);
		}
	}
	else
	{
		if (nd->leftchild == NULL)
		{
			if (nd->rightchild)
			{
				Transplant(nd, nd->rightchild);
			}
			DeleteFixup(nd->parents);
		}
		else if (nd->rightchild == NULL)
		{
			if (nd->leftchild)
			{
				Transplant(nd, nd->leftchild);
			}
			DeleteFixup(nd->parents);
		}
		else
		{
			node* z;
			z = MinChild(nd->rightchild);
			node* x;
			x = z->parents;
			if (z->parents != nd)
			{
				Transplant(z, z->rightchild);
				z->rightchild = nd->rightchild;
				z->rightchild->parents = z;
			}
			else
			{
				x = z;
			}
			Transplant(nd, z);
			z->leftchild = nd->leftchild;
			z->leftchild->parents = z;
			DeleteFixup(x);
		}
	}
	
	
	

}

int AVL::Height(node* nd)
{
	if (nd == NULL)
	{
		return 0;
	}
	else
	{
		return max(Height(nd->leftchild), Height(nd->rightchild)) + 1;
	}
}

void AVL::Transplant(node *u, node *v)
{
	
	if (/*root_node == u*/u->parents == NULL)
	{
		root_node = v;
	}
	else if (u == u->parents->leftchild)
	{
		u->parents->leftchild = v;
	}
	else
	{
		u->parents->rightchild = v;
	}
	if (v)
	{
		v->parents = u->parents;
	}
	
}

void AVL::Insert(node* nd)
{
	node* x = new node;
	x = root_node;
	node* y = NULL;
	
	while (x)
	{
		y = x;
		if (nd->key > x->key)
		{
			x = x->rightchild;
		}
		else
		{
			x = x->leftchild;
		}
	}

	if (y == NULL)
	{
		root_node = nd;
	}
	else
	{
		nd->parents = y;

		if (nd->key > y->key)
		{
			y->rightchild = nd;
		}
		else
		{
			y->leftchild = nd;
		}
	}

	if (nd->parents != NULL)
	{
		InsertFixup(nd->parents);
	}
	
}


void AVL::InsertFixup(node* nd)
{
	node* r;// record_location;
	node* r_p;// parents of r;
	r = nd;
	

	while (r->parents != NULL)
	{
 		r_p = r->parents;

		int r_bf = Height(r->rightchild) - Height(r->leftchild);//可以写成节点的一个属性
		//int rp_bf = Height(r_p->rightchild) - Height(r_p->leftchild);///

		int rp_bf = 0;
		int rr = Height(r_p->rightchild);
		int rl = Height(r_p->leftchild);
		rp_bf = rr - rl;

		if (rp_bf == 2)// 
		{
			if (r_bf == 1)
			{
				LeftRotate(r_p);
			}
			else
			{
				RightRotate(r);
				LeftRotate(r->parents->parents);
				//RLRotate
			}
		}
		else if (rp_bf == -2)
		{
			if (r_bf == 1)
			{
				LeftRotate(r);
				RightRotate(r->parents->parents);
			}
			else
			{
				RightRotate(r_p);
			}
		}

		r = r->parents;
		if (r == NULL)
		{
			break;
		}
	}
}


void AVL::DeleteFixup(node* nd)
{
	while (nd != NULL)
	{
		if (Height(nd->rightchild) - Height(nd->leftchild) == -2)
		{
			if (Height(nd->leftchild->rightchild) - Height(nd->leftchild->leftchild) == -1)
			{
				RightRotate(nd);
			}
			else
			{
				LeftRotate(nd->leftchild);
				RightRotate(nd);
			}
		}
		else if (Height(nd->rightchild) - Height(nd->leftchild) == 2)
		{
			if (Height(nd->rightchild->rightchild) - Height(nd->rightchild->leftchild) == -1)
			{
				RightRotate(nd->rightchild);
				LeftRotate(nd);
			}
		}

		nd = nd->parents;
	}
}


void AVL::LeftRotate(node* nd)
{
	node* y;
	y = nd->rightchild;
	nd->rightchild = y->leftchild;

	if (y->leftchild != NULL)
	{
		y->leftchild->parents = nd;
	}

	y->parents = nd->parents;

	if (nd->parents == NULL)
	{
		root_node = y;
	}
	else if (nd == nd->parents->leftchild)
	{
		nd->parents->leftchild = y;
	}
	else
	{
		nd->parents->rightchild = y;
	}

	y->leftchild = nd;
	nd->parents = y;
}

void AVL::RightRotate(node* nd)
{
	node* y;
	y = nd->leftchild;
	nd->leftchild = y->rightchild;

	if (y->rightchild != NULL)
	{
		y->rightchild->parents = nd;
	}

	y->parents = nd->parents;

	if (nd->parents == NULL)
	{
		root_node = y;
	}
	else if (nd == nd->parents->leftchild)
	{
		nd->parents->leftchild = y;
	}
	else
	{
		nd->parents->rightchild = y;
	}

	y->rightchild = nd;
	nd->parents = y;
}




void PrintTree(node* nd, int nodekey, int direction) //打印以nd为根的子树的所有元素
{
	if (nd != NULL)
	{
		if (direction == 0)
		{
			cout << setw(2) << nd->key << " is root" << endl;
		}
		else
		{
			string s;
			s = direction == 1 ? "right" : "left";
			cout <<setw(2) << nd->key << " is " << setw(2) << nodekey << "'s ";
			cout <<setw(6) <<  s;
			cout << " child" << endl;
		}
		PrintTree(nd->leftchild, nd->key, -1);
		PrintTree(nd->rightchild, nd->key, 1);
	}

}

void testavl()
{
	
	AVL test_tree;
	int nodekey[16] = { 3, 2, 1, 4, 5, 6, 7, 16, 15, 14, 13, 12, 11, 10, 8, 9 };
	node* test_nodes = new node[16];

	for (int i = 0; i < 16; i++)
	{

		test_nodes[i].key = nodekey[i];
		test_nodes[i].leftchild = test_nodes[i].rightchild = test_nodes[i].parents = NULL;
		test_tree.Insert(&test_nodes[i]);
		cout << i << endl;
	}
	
	cout << "生成的AVL为:" << endl;
	PrintTree(test_tree.root_node, test_tree.root_node->key, 0);
	test_tree.Delete(test_tree.root_node);

	cout << "删除根节点之后的AVL为:" << endl;
	PrintTree(test_tree.root_node, test_tree.root_node->key, 0);
	
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AVL树是一种自平衡二叉搜索树,它的每个节点存储一个键值对,且每个节点的左子树和右子树的高度差不超过1。这种平衡特性使得AVL树在查找、插入和删除操作方面都有很好的性能表现。 下面是一个简单的AVL树C++实现: ```c++ #include <iostream> using namespace std; // AVL树节点 struct Node { int key; int height; Node *left; Node *right; Node(int k) : key(k), height(1), left(NULL), right(NULL) {} }; // 获取节点高度 int height(Node *node) { if (node == NULL) { return 0; } return node->height; } // 获取节点平衡因子 int balanceFactor(Node *node) { if (node == NULL) { return 0; } return height(node->left) - height(node->right); } // 更新节点高度 void updateHeight(Node *node) { node->height = max(height(node->left), height(node->right)) + 1; } // 右旋操作 Node* rightRotate(Node *node) { Node *leftChild = node->left; Node *rightChild = leftChild->right; leftChild->right = node; node->left = rightChild; updateHeight(node); updateHeight(leftChild); return leftChild; } // 左旋操作 Node* leftRotate(Node *node) { Node *rightChild = node->right; Node *leftChild = rightChild->left; rightChild->left = node; node->right = leftChild; updateHeight(node); updateHeight(rightChild); return rightChild; } // 插入节点 Node* insert(Node *node, int key) { if (node == NULL) { return new Node(key); } if (key < node->key) { node->left = insert(node->left, key); } else if (key > node->key) { node->right = insert(node->right, key); } else { return node; } updateHeight(node); int bf = balanceFactor(node); if (bf > 1) { if (balanceFactor(node->left) >= 0) { return rightRotate(node); } else { node->left = leftRotate(node->left); return rightRotate(node); } } else if (bf < -1) { if (balanceFactor(node->right) <= 0) { return leftRotate(node); } else { node->right = rightRotate(node->right); return leftRotate(node); } } return node; } // 中序遍历AVL树 void inOrder(Node *node) { if (node == NULL) { return; } inOrder(node->left); cout << node->key << " "; inOrder(node->right); } int main() { Node *root = NULL; root = insert(root, 10); root = insert(root, 20); root = insert(root, 30); root = insert(root, 40); root = insert(root, 50); root = insert(root, 25); inOrder(root); cout << endl; return 0; } ``` 在上面的实现中,我们使用了递归插入节点,并在插入节点后更新了节点的高度和平衡因子。当节点的平衡因子大于1或小于-1时,我们进行相应的旋转操作来保持树的平衡。最后,我们在main函数中插入一些节点,并进行中序遍历来检查树是否正确构建。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值